﻿namespace KeRui.Upload
{
    using System;
    using System.Security.Cryptography;
    using System.Web;

    public class Progress
    {
        private int mCodePage;
        private int mID;
        private StateInfo mInfo;

        internal Progress()
        {
            throw new InvalidOperationException();
        }

        public Progress(int ID)
        {
            Registration.CheckValid();
            try
            {
                this.mID = ID;
                if (HttpContext.Current != null)
                {
                    this.mCodePage = HttpContext.Current.Request.ContentEncoding.CodePage;
                }
                else
                {
                    this.mCodePage = 0;
                }
                this.mInfo = SharedState.Find(this.mID);
            }
            catch (Exception exception)
            {
                if (Settings.Current.Advanced.LogExceptions)
                {
                    Log.ExceptionDetails(exception);
                }
                throw exception;
            }
        }

        public Progress(string ID)
        {
            Registration.CheckValid();
            try
            {
                try
                {
                    this.mID = Convert.ToInt32(ID);
                }
                catch
                {
                    this.mID = 0;
                }
                if (HttpContext.Current != null)
                {
                    this.mCodePage = HttpContext.Current.Request.ContentEncoding.CodePage;
                }
                else
                {
                    this.mCodePage = 0;
                }
                this.mInfo = SharedState.Find(this.mID);
            }
            catch (Exception exception)
            {
                if (Settings.Current.Advanced.LogExceptions)
                {
                    Log.ExceptionDetails(exception);
                }
                throw exception;
            }
        }

        public static int UniqueID()
        {
            int num;
            try
            {
                byte[] data = new byte[4];
                new RNGCryptoServiceProvider().GetNonZeroBytes(data);
                num = Math.Abs((int) (((data[0] + (data[1] << 8)) + (data[2] << 0x10)) + (data[3] << 0x18)));
            }
            catch (Exception exception)
            {
                if (Settings.Current.Advanced.LogExceptions)
                {
                    Log.ExceptionDetails(exception);
                }
                throw exception;
            }
            return num;
        }

        public int BytesDone
        {
            get
            {
                int bytesDone;
                try
                {
                    bytesDone = this.mInfo.BytesDone;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return bytesDone;
            }
        }

        public double BytesPerSecondAverage
        {
            get
            {
                double num;
                try
                {
                    num = this.mInfo.BytesPerSecondAverage();
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return num;
            }
        }

        public double BytesPerSecondCurrent
        {
            get
            {
                double num;
                try
                {
                    num = this.mInfo.BytesPerSecondCurrent();
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return num;
            }
        }

        public int BytesTotal
        {
            get
            {
                int bytesTotal;
                try
                {
                    bytesTotal = this.mInfo.BytesTotal;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return bytesTotal;
            }
        }

        public int CodePage
        {
            get
            {
                int mCodePage;
                try
                {
                    mCodePage = this.mCodePage;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mCodePage;
            }
            set
            {
                try
                {
                    this.mCodePage = value;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
            }
        }

        public double ElapsedSeconds
        {
            get
            {
                double num;
                try
                {
                    num = this.mInfo.ElapsedMS / 1000.0;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return num;
            }
        }

        public string FileName
        {
            get
            {
                string str;
                try
                {
                    str = HTTPUtils.Decode(this.mInfo.FileName, this.mCodePage);
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return str;
            }
        }

        public bool Finished
        {
            get
            {
                bool finished;
                try
                {
                    finished = this.mInfo.Finished;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return finished;
            }
        }

        public int ID
        {
            get
            {
                int mID;
                try
                {
                    mID = this.mID;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mID;
            }
            set
            {
                try
                {
                    this.mID = value;
                    this.mInfo = SharedState.Find(this.mID);
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
            }
        }

        public static int[] IDs
        {
            get
            {
                int[] iDs;
                try
                {
                    iDs = SharedState.GetIDs();
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return iDs;
            }
        }

        public string Note
        {
            get
            {
                string note;
                try
                {
                    note = this.mInfo.Note;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return note;
            }
            set
            {
                try
                {
                    this.mInfo.Note = value;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
            }
        }

        public double PercentDone
        {
            get
            {
                double num;
                try
                {
                    num = this.mInfo.PercentDone();
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return num;
            }
        }

        public double SecondsLeft
        {
            get
            {
                double num;
                try
                {
                    num = this.mInfo.SecondsLeft();
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return num;
            }
        }

        public bool Started
        {
            get
            {
                bool started;
                try
                {
                    started = this.mInfo.Started;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return started;
            }
        }
    }
}

